package com.ticket.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XMLUtil {
	public static String getRootElement(String xml) throws DocumentException {
		Document document = DocumentHelper.parseText(xml);
		Element root = document.getRootElement();
		return root.getName();
	}

	public static Map<String, String> parseTomap(String xml)
			throws DocumentException {
		Document document = DocumentHelper.parseText(xml);
		Element root = document.getRootElement();
		return parseTomap(root);
	}

	private static Map<String, String> parseTomap(Element element) {
		Map map = new HashMap();
		map.put(element.getName(), element.getTextTrim());
		if (element.nodeCount() > 0) {
			for (Iterator i = element.elementIterator(); i.hasNext();) {
				Element sub = (Element) i.next();
				Map subMap = parseTomap(sub);
				map.putAll(subMap);
			}
		}
		return map;
	}
}